import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonGUI extends JFrame {
	
	static JTextField firstNameTextField;
	static JTextField lastNameTextField;
	static JTextField ageTextField;
	static JTextField ssnTextField;
	
	public PersonGUI() {
		setLayout(new GridLayout(5, 2, 5, 5));
		setTitle("Person");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300,200);
		
		add(new JLabel(" First Name: "));
		firstNameTextField = new JTextField();
		add(firstNameTextField);
		
		add(new JLabel(" Last Name: "));
		lastNameTextField = new JTextField();
		add(lastNameTextField);
		
		add(new JLabel(" Age: "));
		ageTextField = new JTextField();
		add(ageTextField);
		
		add(new JLabel(" SSN: "));
		ssnTextField = new JTextField();
		add(ssnTextField);
		
		JButton submit = new JButton("Submit");
		add(submit);
		PersonSubmitListener listener1 = new PersonSubmitListener();
		submit.addActionListener(listener1);
		
		
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new PersonCancelListener());
		add(cancel);
	}
}

class PersonSubmitListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
		Person p;
		int age = Integer.parseInt(PersonGUI.ageTextField.getText());
	    p = new Person(PersonGUI.firstNameTextField.getText(), PersonGUI.lastNameTextField.getText(), age, PersonGUI.ssnTextField.getText());
	    System.out.println(p.toString());
	    Main.chooserframe.setVisible(true);
		Main.personframe.setVisible(false);
		PersonGUI.firstNameTextField.setText("");
		PersonGUI.lastNameTextField.setText(""); 
		PersonGUI.ageTextField.setText(""); 
		PersonGUI.ssnTextField.setText("");
	}
}

class PersonCancelListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
		Main.chooserframe.setVisible(true);
		Main.personframe.setVisible(false);
		PersonGUI.firstNameTextField.setText("");
		PersonGUI.lastNameTextField.setText(""); 
		PersonGUI.ageTextField.setText(""); 
		PersonGUI.ssnTextField.setText("");
	}
}
